home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / EXAMPLES / PATCHES.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  5KB  |  280 lines

  1.  
  2. #include <stdio.h>
  3.  
  4. #ifdef SGI
  5. #include "gl.h"
  6. #include "device.h"
  7. #else
  8. #include "vogl.h"
  9. #include "vodevice.h"
  10. #endif
  11.  
  12. /*
  13.  *    Draws patches of various bases
  14.  */
  15.  
  16. /*
  17.  * patch basis types
  18.  */
  19.  
  20. Matrix    bezier = {
  21.     {-1.0,    3.0,    -3.0,    1.0},
  22.     {3.0,    -6.0,    3.0,    0.0},
  23.     {-3.0,    3.0,    0.0,    0.0},
  24.     {1.0,    0.0,    0.0,    0.0} 
  25. };
  26.  
  27. Matrix    cardinal = { /* Or catmull-rom */
  28.     {-0.5,    1.5,    -1.5,    0.5},
  29.     {1.0,    -2.5,    2.0,    -0.5},
  30.     {-0.5,    0.0,    0.5,    0.0},
  31.     {0.0,    1.0,    0.0,    0.0}
  32. };
  33.  
  34. Matrix    bspline = {
  35.     {-1.0 / 6.0,    3.0 / 6.0,    -3.0 / 6.0,    1.0 / 6.0},
  36.     {3.0 / 6.0,    -6.0 / 6.0,    3.0 / 6.0,    0.0},
  37.     {-3.0 / 6.0,    0.0,        3.0 / 6.0,    0.0},
  38.     {1.0 / 6.0,    4.0 / 6.0,    1.0 / 6.0,    0.0}    
  39. };
  40.  
  41. Matrix    power = {
  42.     {1.0, 0.0, 0.0, 0.0},
  43.     {0.0, 1.0, 0.0, 0.0},
  44.     {0.0, 0.0, 1.0, 0.0},
  45.     {0.0, 0.0, 0.0, 1.0}
  46. };
  47.  
  48. float *basis[] = {    (float *)bezier,
  49.             (float *)cardinal,
  50.             (float *)bspline,
  51.             (float *)power
  52.         };
  53.  
  54. Matrix    x1 = {
  55.     {0.0,   0.2588,   0.5,   0.7071},
  56.     {0.0,   0.51764,  1.0,   1.4142},
  57.     {0.0,   0.51764,  1.0,   1.4142},
  58.     {0.0,   0.2588,   0.5,   0.7071}
  59. };
  60.  
  61. Matrix    y1 = {
  62.     {1.0,   0.966,   0.866,  0.7071},
  63.     {2.0,   1.9318,  1.732,  1.4142},
  64.     {2.0,   1.9318,  1.732,  1.4142},
  65.     {1.0,   0.966,   0.866,  0.7071}
  66. };
  67.  
  68. Matrix    z1 = {
  69.     {1.0,   1.0,     1.0,    1.0},
  70.     {1.0,   1.0,     1.0,    1.0},
  71.     {0.0,   0.0,     0.0,    0.0},
  72.     {0.0,   0.0,     0.0,    0.0}
  73. };
  74.  
  75. Matrix    x2 = {
  76.     {0.7071, 0.8660, 0.9660, 1.0},
  77.     {1.4142, 1.7320, 1.932,  2.0},
  78.     {1.4142, 1.7320, 1.932,  2.0},
  79.     {0.7071, 0.8660, 0.9660, 1.0}
  80. };
  81.  
  82. Matrix    y2 = {
  83.     {0.7071, 0.5,    0.2588, 0.0},
  84.     {1.4142, 1.0,    0.5176, 0.0},
  85.     {1.4142, 1.0,    0.5176, 0.0},
  86.     {0.7071, 0.5,    0.2588, 0.0}
  87. };
  88.  
  89. Matrix    z2 = {
  90.     {1.0,   1.0,     1.0,    1.0},
  91.     {1.0,   1.0,     1.0,    1.0},
  92.     {0.0,   0.0,     0.0,    0.0},
  93.     {0.0,   0.0,     0.0,    0.0}
  94. };
  95.  
  96. char *labels[] = {
  97.         "Bezier Patch(es)",
  98.         "Cardinal Patch(es)",
  99.         "B-Spline Patch(es)",
  100.         "'Power' Patch(es)"
  101.         };
  102.  
  103. void    axes(), drawhull();
  104. /*
  105.  * demonstrate patches
  106.  */
  107.  
  108. main()
  109. {
  110.     int    i;
  111.     short    val;
  112.     Screencoord    minx, maxx, miny, maxy;
  113.  
  114.     hfont("times.r");
  115.  
  116.     winopen("patches");
  117.  
  118.     unqdevice(INPUTCHANGE);
  119.     qdevice(KEYBD);
  120.  
  121.     color(BLACK);
  122.     clear();
  123.  
  124.     /*
  125.      * Set up two viewports (They actually overlap)
  126.      */
  127.     getviewport(&minx, &maxx, &miny, &maxy);
  128.  
  129.     viewport(minx, (maxx - minx) / 10 * 6, miny, (maxy - miny) / 10 * 6);
  130.     ortho(-2.0, 5.0, -2.0, 5.0, -2.0, 5.0);
  131.     lookat(0.0, 0.0, 0.0, -3.0, 2.0, -4.0, 0);
  132.     /*
  133.      * Save it 
  134.      */
  135.     pushviewport();
  136.     pushmatrix();
  137.  
  138.     viewport((maxx - minx) / 10 * 2, maxx, (maxy - miny) / 10 * 2, maxy);
  139.     ortho(-2.0, 5.0, -2.0, 5.0, -2.0, 5.0);
  140.     lookat(0.0, 0.0, 0.0, 3.0, 2.0, -4.0, 0);
  141.  
  142.     htextsize(0.4, 0.4);
  143.  
  144.     /*
  145.      * patchcurves provides a number of curves in the t and u
  146.      * directions. patchprecision gives the minimum number of line
  147.      * segments making up the curves in the t and u directions. The
  148.      * actual number of linesegments in t or u is equal to the closest
  149.      * integer multiple of the number of curves, > nsegs, in t or u,
  150.      * greater than or equal to the number set by patchprecision in u or
  151.      * t. eg. curves in t will be made up of 21 line segments so that we
  152.      * can match up the 7 curves in u; curves in u will have 24 as 4 by 5
  153.      * gives 20.
  154.      */
  155.     patchcurves(4L, 7L);
  156.     patchprecision(20L, 20L);
  157.  
  158.     for (i = 0; i < 4; i++) {
  159.  
  160.         axes();
  161.  
  162.  
  163.         /*
  164.          * patchbasis sets the basis matrices for the t and u
  165.          * functions
  166.          * 
  167.          */
  168.         defbasis((short)i, basis[i]);
  169.         patchbasis((long)i, (long)i);
  170.  
  171.         /* 
  172.          * Draw with viewport 2
  173.          */
  174.         move(0.0, 4.0, 0.0);
  175.         hcharstr(labels[i]);
  176.  
  177.         /*
  178.          * now draw the patches according to the geometry matrices in
  179.          * x1, y1, and z1, x2, y2, z2.
  180.          */
  181.         drawhull(x1, y1, z1);
  182.         patch(x1, y1, z1);
  183.  
  184.         drawhull(x2, y2, z2);
  185.         patch(x2, y2, z2);
  186.  
  187.         /*
  188.          * Now with viewport 1
  189.          */
  190.         popviewport();
  191.         popmatrix();
  192.  
  193.         axes();
  194.  
  195.         move(0.0, 4.0, 0.0);
  196.         hcharstr(labels[i]);
  197.  
  198.         /*
  199.          * now draw the patches according to the geometry matrices in
  200.          * x1, y1, and z1, x2, y2, z2.
  201.          */
  202.         drawhull(x1, y1, z1);
  203.         patch(x1, y1, z1);
  204.  
  205.         drawhull(x2, y2, z2);
  206.         patch(x2, y2, z2);
  207.  
  208.         qread(&val);
  209.  
  210.         /*
  211.          * Save viewport 1, after clearing it, again and reset
  212.          * to viewport 2
  213.          */
  214.         color(BLACK);
  215.         clear();
  216.  
  217.         pushviewport();
  218.         pushmatrix();
  219.  
  220.         viewport((maxx - minx) / 10 * 2, maxx, (maxy - miny) / 10 * 2, maxy);
  221.         ortho(-1.5, 5.0, -1.5, 5.0, -1.5, 5.0);
  222.         lookat(0.0, 0.0, 0.0, 3.0, 2.0, -4.0, 0);
  223.  
  224.         clear();
  225.     }
  226.  
  227.     gexit();
  228. }
  229.  
  230. /*
  231.  * drawhull
  232.  *
  233.  *    draw the hull for x, y, and z.
  234.  */
  235. void
  236. drawhull(x, y, z)
  237.     Matrix    x, y, z;
  238. {
  239.     int    i, j;
  240.     char    buf[2];
  241.  
  242.     color(MAGENTA);    
  243.  
  244.     for (i = 0; i < 4; i++) {
  245.         move(x[i][0], y[i][0], z[i][0]);
  246.         for (j = 1; j < 4; j++)
  247.             draw(x[i][j], y[i][j], z[i][j]);
  248.     }
  249.  
  250.     for (i = 0; i < 4; i++) {
  251.         move(x[0][i], y[0][i], z[0][i]);
  252.         for (j = 1; j < 4; j++) 
  253.             draw(x[j][i], y[j][i], z[j][i]);
  254.     }
  255.  
  256.     /* 
  257.       * Set color for The patch
  258.      */
  259.     color(GREEN);
  260. }
  261.  
  262. /*
  263.  * axes
  264.  *
  265.  *    draw the axes
  266.  */
  267. void
  268. axes()
  269. {
  270.     color(YELLOW);
  271.     move(0.0, 0.0, 0.0);
  272.     draw(4.0, 0.0, 0.0);
  273.  
  274.     move(0.0, 0.0, 0.0);
  275.     draw(0.0, 4.0, 0.0);
  276.  
  277.     move(0.0, 0.0, 0.0);
  278.     draw(0.0, 0.0, 4.0);
  279. }
  280.